package com.csd.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.csd.base.BaseFragment;
import com.csd.fragment.EmployeeFragment;
import com.csd.fragment.IndexFragment;
import com.csd.fragment.OrderFragment;

public class MainActivity extends FragmentActivity {

	private RadioGroup rg;
	private CsdApplication csdApp;
	private List<BaseFragment> mFragments;
	//退出程序时第一次点击返回键的时间
	private long startTime;
	private ViewPager mViewPager;
	private ActionBar actionBar;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		Intent intent1 = this.getIntent();
		int flag = intent1.getIntExtra("flag", 1);
		if(flag == 1){
			showDialog();
		}
		
		 actionBar = getActionBar();
		actionBar.hide();
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		// TODO Auto-generated method stub
		if(csdApp == null){
		csdApp = CsdApplication.getInstance();
		}
		csdApp.addActivity(this);
		//将首页，我的订单，我的资料fragment添加到MainActivity中
		mFragments = new ArrayList<BaseFragment>();
		mFragments.add(new IndexFragment());
		mFragments.add(new OrderFragment());
		mFragments.add(new EmployeeFragment());
		mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
		rg = (RadioGroup) findViewById(R.id.rg);
		Intent intent = getIntent();
		//判断跳转到那个界面
		int num = intent.getIntExtra("num", 0);
		switch (num) {
		case 0:
			//首页
			rg.check(R.id.rb_index);
			mViewPager.setCurrentItem(0);
			break;
		case 1:
			//我的订单
			rg.check(R.id.rb_order);
			mViewPager.setCurrentItem(1);
			break;
		case 2:
			//我的资料
			rg.check(R.id.rb_material);
			mViewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
		
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_index:
					mViewPager.setCurrentItem(0);
					break;
				case R.id.rb_order:
					mViewPager.setCurrentItem(1);
					break;
				case R.id.rb_material:
					mViewPager.setCurrentItem(2);
					break;
				}
			}
		});
		
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					rg.check(R.id.rb_index);
					break;
				case 1:
					rg.check(R.id.rb_order);
					break;
				case 2:
					rg.check(R.id.rb_material);
					break;
				}				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
	}

	private class MyAdapter extends FragmentPagerAdapter {

		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return mFragments.get(arg0);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (System.currentTimeMillis() - startTime < 2000) {
				csdApp.exit();
			} else {
				Toast.makeText(MainActivity.this, "再按一下就退出", Toast.LENGTH_SHORT)
						.show();
				startTime = System.currentTimeMillis();
			}
		}
		return true;

	}

	private void showDialog() {
		// TODO Auto-generated method stub
		final Dialog dialog = new Dialog(this,R.style.Dialog);
        //设置它的ContentView
		View v = LayoutInflater.from(this).inflate(R.layout.dailog_time, null);
		dialog.setContentView(v);
		Button bt_look = (Button) v.findViewById(R.id.look);
		Button bt_book = (Button) v.findViewById(R.id.book);
		bt_book.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, EnterpriseApplayActivity.class);
				startActivity(intent);
				dialog.dismiss();
			}
		});
		bt_look.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			dialog.dismiss();	
			}
		});
        dialog.show();
	}


}
